<template>
    <div>
        <div class="register_con">
            <div class="l_con fl">
                <a href="index.html" class="reg_logo"><img src="/static/images/logo.png"></a>
                <div class="reg_slogan">商品美 · 种类多 · 欢迎光临</div>
                <div class="reg_banner"></div>
            </div>

            <div class="r_con fr">
                <div class="reg_title clearfix">
                    <h1>用户注册</h1>
                    <a href="/login.html">登录</a>
                </div>
                <div class="reg_form clearfix" id="app" v-cloak>
                    <form method="post" @submit.prevent="register">
                        
                        <ul>
                            <li>
                                <label>用户名:</label>
                                <input  v-model="username" type="text" name="username" id="user_name" >
                                <!-- <span v-show="error_name" class="error_tip">{{ error_name_message }}</span> -->
                            </li>
                            <li>
                                <label>密码:</label>
                                <input v-model="password1"  type="password" name="password" id="pwd">
                                <!-- <span v-show="error_password" class="error_tip">请输入8-20位的密码</span> -->
                            </li>
                            <li>
                                <label>确认密码:</label>
                                <input  v-model="password2" type="password" name="password2" id="cpwd">
                                <!-- <span v-show="error_check_password" class="error_tip">两次输入的密码不一致</span> -->
                            </li>
                            <li>
                                <label>邮箱:</label>
                                <input  v-model="email" type="text" name="email" >
                                <!-- <span v-show="error_phone" class="error_tip">{{ error_phone_message }}</span> -->
                            </li>
                            <li>
                                <label>图形验证码:</label>
                                <input type="text" v-model="image_code"  name="image_code"
                                    id="pic_code" class="msg_input">
                                <img :src="img_url+image_id"  alt="图形验证码" class="pic_code" @click="get_uuid">
                                <!-- <span v-show="error_image_code" class="error_tip">{{ error_image_code_message }}</span> -->
                            </li>
                            <!-- <li>
                                <label>短信验证码:</label>
                                <input type="text" v-model="sms_code" @blur="check_sms_code" name="sms_code" id="msg_code"
                                    class="msg_input">
                                <a @click="send_sms_code" class="get_msg_code">{{ sms_code_tip }}</a>
                                <span class="error_tip" v-show="error_sms_code">{{ error_sms_code_message }}</span>
                            </li> -->
                            <li class="agreement">
                                <input v-model="allow" type="checkbox" name="allow" id="allow" >
                                <label>同意”美多商城用户使用协议“</label>
                                <!-- <span v-show="error_allow" class="error_tip">请勾选用户协议</span> -->
                            </li>
                            <li class="reg_sub">
                                <input type="submit" value="注 册" >
                            </li>
                        </ul>
                    </form>
                </div>
            </div>
        </div>

        <!-- 使用组件 -->
        <myfooder></myfooder>
    </div>
</template>

<script>
import axios from 'axios'
// 导入组件
import myfooder from '../components/Fooder'
export default {
    // 注册组件
    components: {
        'myfooder': myfooder
    },
    data() {
        return {
            username: '',
            password1: '',
            password2: '',
            email: '',
            allow: 1,
            image_id: '',
            image_code: '',
            // 错误提示
            error_name: false,
            img_url: 'http://127.0.0.1:8000/users/verify_code?image_id=',
            error_code: false,
            base_url:'http://127.0.0.1:8000'
        }
    },
    methods: {
        check_code() {
            axios({
                url: this.base_url + '/users/check_code',
                params: {'image_id': this.image_id, 'image_code': this.image_code}
            }).then(res => {
                console.log(res.data)
                if(res.data.code == 400) {
                    this.error_code = true
                }else{
                    this.error_code = false
                }
            })
        },
        // 获取uuid
        get_uuid() {
            // 发送get请求，获取uuid
            axios({
                url: this.base_url + '/users/create_uuid'
            }).then(res => {
                console.log(res.data)
                this.image_id = res.data.image_id
            })
        },
        // 检查用户名
        // check_username() {
        //     axios({
        //         url: this.base_url + '/user/check_username/?username=' + this.username
        //     }).then(res => {
        //         console.log(res.data)
        //         if(res.data.code == 200) {
        //             this.error_name = false
        //         }else{
        //             this.error_name = true
        //         }
        //     })
        // },
        // 注册
        register() {
            // 1、实例化
            let form_data = new FormData()
            // 2、添加数据
            form_data.append('username', this.username)
            form_data.append('password1', this.password1)
            form_data.append('password2', this.password2)
            form_data.append('email', this.email)
            // 3、发送数据
            axios({
                url: this.base_url + '/users/register',
                method: 'post',
                data: form_data
            }).then(res => {
                console.log(res)
                console.log(res.data)
            })

        }
    },
    created() {
        // 页面载时，调用uuid方法
        this.get_uuid()
    }
}
</script>

<style scoped>

</style>
